import re


class StringUtil:
    """字符串工具类"""

    @classmethod
    def to_snake_case(cls, name: str) -> str:
        """驼峰命名转蛇形命名"""
        to_snake_pattern = re.compile(r'(?<!^)(?=[A-Z])')
        return to_snake_pattern.sub('_', name).lower()

    @classmethod
    def to_camel_case(cls, name: str, is_upper=True) -> str:
        """蛇形命名转驼峰命名
            is_upper: 为True则首字母大写, 否则首字母小写
        """
        if not name:
            return name
        res = ''.join(word.title() for word in name.split('_'))
        if not is_upper:
            res = f'{res[0].lower()}{res[1:]}'
        return res

    @classmethod
    def convert_to_number_regex(cls, s: str):
        """
        使用正则表达式判断并转换字符串为数字。
        """
        if re.fullmatch(r'-?\d+', s):  # 匹配整数（可带负号）
            return int(s)
        elif re.fullmatch(r'-?\d+\.\d+', s):  # 匹配浮点数（可带负号）
            return float(s)
        return s
